package com.example.controller;

import com.example.dto.LoginDto;
import com.example.dto.OrdersDto;
import com.example.dto.OrdersListDto;
import com.example.dto.ReportListDto;
import com.example.pojo.Hospital;
import com.example.pojo.Orders;
import com.example.pojo.Setmeal;
import com.example.pojo.User;
import com.example.service.HospitalService;
import com.example.service.OrderService;
import com.example.service.SetmealService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/confirmorder")
public class OrdersController {
    @Autowired
    private OrderService orderService;

    /**
     * 返回订单详情信息
     * 新增了医生的插入
     * @param orders
     * @return
     */
    @PostMapping("/order")
    public ResponseEntity<Map<String, Object>> getOrder(@RequestBody OrdersDto orders) {
        return orderService.getOrder(orders);
    }

    /**
     * 创建订单
     *新增了医生的插入
     * @param
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> confirmOrder(@RequestBody OrdersDto ordersDto) {
        return orderService.insertOrder(ordersDto);
    }


    /**
     * 我的预约列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public Map<String, Object> getOrdersByUserId(@RequestParam("userId") String userId) {
        List<OrdersListDto> orders = orderService.getOrdersByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        if(orders != null && !orders.isEmpty()){
            map.put("code",200);
            map.put("msg", "success");
            map.put("orders", orders);
            map.put("total", orders.size());
        }else{
            map.put("code",400);
            map.put("msg", "fail");
            map.put("orders", null);
            map.put("total", 0);
        }
        return map;
    }
    /**
     * 取消预约
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public ResponseEntity<Map<String, Object>> cancelOrder(@RequestParam("orderId") Integer orderId) {
        // Check if orderId exists in the database
        boolean isOrderExists = orderService.checkOrderExists(orderId);

        if (!isOrderExists) {
            // If orderId doesn't exist, return HTTP 400 with JSON response
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "Order with orderId=" + orderId + " does not exist.");
            return ResponseEntity.badRequest().body(response);
        }

        // Cancel the order if it exists
        orderService.cancelOrder(orderId);

        // Return HTTP 200 OK on successful cancellation
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "Order cancelled successfully");
        return ResponseEntity.ok(response);
    }
}
